package wx.display
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	
	/**
	 * 
	 * @author wujianqi
	 * 
	 * 边框、填充背景图或颜色
	 * 
	 */	
	public class BorderSprite extends Sprite
	{
		private var _width:Number = 0;       
		private var _height:Number = 0;
		private var _bitmap:Bitmap;
		private var _color:Object;		
		
		public var borderWeight:int = 1;
		public var borderColor:uint = 0x000000;
		public var borderVisible:Boolean = false;
		
		public function BorderSprite($bgColor:Object = null,$width:Number = 0, $height:Number = 0)
		{
			super();
			_color = $bgColor;
			_width = $width;
			_height = $height;
			draw();
		}
		
		/**
		 * 
		 * @param bmp
		 * 
		 * 平铺背景图
		 * 
		 */		
		public function set backgroundImage(bmp:Bitmap):void{
			if(_bitmap != bmp){
				_bitmap = bmp;
				draw();
			}
		}
		
		/**
		 * 
		 * @param bgColor
		 * 
		 * 背景颜色
		 * 
		 */		
		public function set backgroundColor(bgColor:Object):void{
			if(_color != bgColor){
				_color = bgColor;
				draw();
			}
		}
		
		override public function get width():Number {
			return _width;
		}       
		override public function set width(value:Number):void{
			if(_width != value){
				_width = value;
				draw();       
			}
		}             
		override public function get height():Number {      
			return _height;       
		}       
		override public function set height(value:Number):void{
			if(_height != value){
				_height = value;
				draw();       
			}
		} 
		
		private function draw():void {
			if(_width>0 && _height>0) {
				graphics.clear();
				if(borderVisible) graphics.lineStyle(borderWeight,borderColor);
				if(_color!=null) graphics.beginFill(_color as uint);
				if(_bitmap!=null){
					graphics.beginBitmapFill(_bitmap.bitmapData,null, true, true);
				}
				graphics.drawRect(this.x,this.y,_width,_height);
				graphics.endFill();
			}
		}
		
		
	}
}